package com.itheima.d7_thread_pool;

import java.util.concurrent.*;

/**
 * 目标：掌握线程池的创建。
 * |---线程池执行Callable任务执行
 */
public class ThreadPoolTest2 {
    public static void main(String[] args) throws Exception {
        // 1、通过ThreadPoolExecutor创建一个线程池对象。
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                5, 10, 30, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10)
                , Executors.defaultThreadFactory()
                , new ThreadPoolExecutor.AbortPolicy()
        );
        Future<Integer> future = pool.submit(new MyCallableTask(100));
        Integer sum = future.get();
        System.out.println("sum = " + sum);

        //线程池一般不关
        //pool.shutdown();
    }
}
class MyCallableTask implements Callable<Integer> {

    private int n;

    public MyCallableTask(int n) {
        this.n = n;
    }

    @Override
    public Integer call() throws Exception {
        int sum =0;
        for (int i = 0; i <= n; i++) {
            sum +=i;
        }
        return sum;
    }
}
